#include "FolderSelect.h"
#include <tchar.h>

namespace
{
	int CALLBACK BrowseProc(
		HWND hwnd,
		UINT uMsg,
		LPARAM lParam,
		LPARAM lpData)
	{
		switch (uMsg)
		{
			case BFFM_INITIALIZED:
				::SendMessage(hwnd, BFFM_SETSELECTION, 1, lpData);
				break;
			default:
				break;
		}
		return 0;
	}
}

void CFolderSelect::Init(HWND hwnd, LPCTSTR pszInitFolder, LPCTSTR title/* =NULL */)
{
	bi.hwndOwner = hwnd;
	bi.lpszTitle = title;
	bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
	bi.lpfn = BrowseProc;
	bi.lParam = (LPARAM)szPath;

	SetSelectedFolder(pszInitFolder);
}

BOOL CFolderSelect::Select()
{
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
	if (pidl)
	{
		SHGetPathFromIDList(pidl, szPath);
		return TRUE;
	}

	return FALSE;
}